/*
 * Copyright (C) 2011 Consulanza Informatica.
 * info@consulanza.it
 *
 * This file is part of Customer Calls Tracker.
 *
 *    Customer Calls Tracker is free software: you can redistribute it and/or
 *    modify it under the terms of the GNU General Public License as published
 *    by the Free Software Foundation, either version 3 of the License, or (at 
 *    your option) any later version.
 *
 *    Customer Calls Tracker is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
 *    Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with Customer Calls Tracker. If not, see http://www.gnu.org/licenses/.
 */

package consulanza.amlsoft.customercalls;


import com.marcoduff.util.aboutactivity.AboutActivity;
import com.marcoduff.util.aboutactivity.R;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;


public class MenupanelActivity extends Activity implements AboutActivity.OnStartupEulaListener, AboutActivity.OnStartupChangeLogListener
{
    private static final String TAG = "CustomerCalls Menupanel";
	/*
	 * form fields
	 */
    private Button mBtnList;
    private Button mBtnExportXml;
    private Button mBtnImportXml;
    private Button mBtnInsert;
    private Button mBtnDeleteBilled;
    /*
     * 
     */

    
	private CustomerCallsDbAdapter mDbHelper;
    /**
     * start activity for Call Create
     */
    private void createCall() {
        Intent i = new Intent(this, CallEdit.class);
        startActivityForResult(i, InterfaceIds.ACTIVITY_CREATE);
    }
    private	void startAboutActivity() {
    	Intent intent = AboutActivity.getAboutActivityIntent(
    			this, "Amedeo Lanza di Casalanza", "http://www.inforisorse.it/", "info@consulanza.it", null,
    			"https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=P8ZA5X4BMZG5J",
    			false, null,
    			null);
    	startActivity(intent);    	
    }
	//@Override
	public void onEulaAction(boolean isAccepted) {
		if(isAccepted) Toast.makeText(this, "Eula accepted", Toast.LENGTH_SHORT).show();
		else if(isAccepted) Toast.makeText(this, "Eula refused", Toast.LENGTH_SHORT).show();
	}

	//@Override
	public void onVersionChanged(String oldVersion, String newVersion) {
		Toast.makeText(this, String.format("Versione cambiata da %1$s a %2$s", oldVersion, newVersion), Toast.LENGTH_SHORT).show();
	}

    /**
     * export database to XML file
     */
    private void exportXml() {
    	mDbHelper.exportXml();
    	showDialog(InterfaceIds.DIALOG_ID_EXPORT_XML_DONE);
    }
    /**
     * delete billed calls
     */
    private void deleteBilled() {
    	mDbHelper.deleteBilled(true);
    }
    /**
     * import database from XML file
     */
    private void importXml() {
    	Intent i = new Intent(this, FileDialog.class);
    	i.putExtra(FileDialog.START_PATH, Common.PATH_SDCARD);
    	i.putExtra(FileDialog.SELECTION_MODE, SelectionMode.MODE_OPEN);
    	startActivityForResult(i, InterfaceIds.ACTIVITY_FILEDLG);
    }
    /**
     * import database from XML file
     */
    private void doImportXml(String filePath) {
    	mDbHelper.importXml(filePath);
    }
    /**
     * start activity for LIST
     */
    private void listCalls() {
		Intent myIntent = new Intent(MenupanelActivity.this, CallList.class);
		MenupanelActivity.this.startActivity(myIntent);
    }
    /**
     * Process activity results
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        switch (requestCode) {
	        case InterfaceIds.ACTIVITY_CREATE:
	        	return;
	        case InterfaceIds.ACTIVITY_FILEDLG:
	        	onFiledialogResult(resultCode, intent);
	        	return;
        }
    }
    /**
     * Callback for FileDialog activity (import). If a file was selected then
     * starts importing it into DB
     * 
     * @param resultCode
     * @param data
     */
    private void onFiledialogResult(int resultCode, Intent data) {
    	switch (resultCode) {
	    	case Activity.RESULT_OK:
	            String filePath = data.getStringExtra(FileDialog.RESULT_PATH);
	            doImportXml(filePath);
	        	showDialog(InterfaceIds.DIALOG_ID_IMPORT_XML_DONE);
	            break;
	    	case Activity.RESULT_CANCELED:
	    	    Log.w(TAG,this.getString(R.string.warn_no_file_selected));
	    		break;
        }
    }
    /**
     * setup control events
     */
	public void setupControlEvents() {
		mBtnList.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				listCalls();
			}
		});
		mBtnInsert.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				createCall();
			}
		});
		mBtnExportXml.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				exportXml();
			}
		});
		mBtnImportXml.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				importXml();
			}
		});
		mBtnDeleteBilled.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				deleteBilled();
			}
		});
		
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
    	AboutActivity.showStartupEula(this);
    	AboutActivity.showStartupChangeLog(this);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.menu_panel);
		// menu buttons
		mBtnList 			= (Button) findViewById(R.id.btn_list);
		mBtnExportXml 		= (Button) findViewById(R.id.btn_export_xml);
		mBtnImportXml 		= (Button) findViewById(R.id.btn_import_xml);
		mBtnInsert 			= (Button) findViewById(R.id.btn_insert_call);
		mBtnDeleteBilled	= (Button) findViewById(R.id.btn_delete_billed);
		// DB adapter
        mDbHelper 		= new CustomerCallsDbAdapter(this);
        mDbHelper.open();
        setupControlEvents();
	}
    /**
     * Setup application menu
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, InterfaceIds.MENU_ID_LIST, 			0, R.string.menu_list_calls);
        menu.add(0, InterfaceIds.MENU_ID_INSERT, 		1, R.string.menu_insert_call);
        menu.add(0, InterfaceIds.MENU_ID_EXPORT_XML, 	2, R.string.menu_export_xml);
        menu.add(0, InterfaceIds.MENU_ID_IMPORT_XML, 	3, R.string.menu_import_xml);
        menu.add(0, InterfaceIds.MENU_ID_DELETE_BILLED,	4, R.string.menu_delete_billed);
        menu.add(0, InterfaceIds.MENU_ID_ABOUT,			5, R.string.menu_about);
        return true;
    }
    /**
     * Setup application menu actions
     */
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch(item.getItemId()) {
            case InterfaceIds.MENU_ID_INSERT:
                createCall();
                return true;
            case InterfaceIds.MENU_ID_LIST:
                listCalls();
                return true;
            case InterfaceIds.MENU_ID_EXPORT_XML:
                exportXml();
                return true;
            case InterfaceIds.MENU_ID_IMPORT_XML:
                importXml();
                return true;
            case InterfaceIds.MENU_ID_DELETE_BILLED:
                deleteBilled();
            case InterfaceIds.MENU_ID_ABOUT:
                startAboutActivity();
                return true;
        }

        return super.onMenuItemSelected(featureId, item);
    }
    /**
     * handle dialogs creation
     */
    @Override
    protected Dialog onCreateDialog(int id) {
        switch(id) {
	        case InterfaceIds.DIALOG_ID_EXPORT_XML_DONE:
	        	return CommonDialogs.NotificationDialog(this,R.string.title_info, R.string.inf_export_xml_done);
	        //
	        case InterfaceIds.DIALOG_ID_IMPORT_XML_DONE:
	        	return CommonDialogs.NotificationDialog(this,R.string.title_info, R.string.inf_import_xml_done);
	        default:
	        	return null;
        }
    }
}
